function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}


$(function () {
    $(".del_commodity").click(function(){
        var commodity_id=$(this).attr('commodity_id');
        var merchant_id=$(this).attr('merchant_id');
        var params={
            "commodity_id":commodity_id,
            "merchant_id":merchant_id
        }
        $.ajax({
            url: "/user_index_commodity_management/del",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    // 修改成功
                    alert("删除成功")
                    window.location.reload()
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })

    $(".release_commodity_form").submit(function(e){
        e.preventDefault()
        var commodity_name = $("#commodity_name").val();
        var classify_id = $("#classify_id").val();
        var unit_price = $("#unit_price").val();
        var abstract = $("#abstract").val();
        var upload_file = $("#upload_file").val();

        if (!commodity_name) {
            alert('请输入商品名称!')
            return
        }

        if (!classify_id) {
            alert('请选择分类!')
            return
        }

        if (!unit_price) {
            alert('请输入商品单价!')
            return
        }

        if (!abstract) {
            alert('请输入商品的描述!')
            return
        }

        if (!upload_file) {
            alert('请上传图片!')
            return
        }
        $(this).ajaxSubmit({
            url: "/user_index_commodity_management_release/update",
            type: "post",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            success: function (resp) {
                if (resp.errno == "0") {
                    // 修改成功
                    alert("修改成功")
                    window.location.reload()
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })

    $(".update_commodity_form").submit(function(e){
        e.preventDefault()
        var commodity_name = $("#commodity_name").val();
        var classify_id = $("#classify_id").val();
        var unit_price = $("#unit_price").val();
        var abstract = $("#abstract").val();

        if (!commodity_name) {
            alert('请输入商品名称!')
            return
        }

        if (!classify_id) {
            alert('请选择分类!')
            return
        }

        if (!unit_price) {
            alert('请输入商品单价!')
            return
        }

        if (!abstract) {
            alert('请输入商品的描述!')
            return
        }
        $(this).ajaxSubmit({
            url: "/user_index_commodity_management/modity/update",
            type: "post",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            success: function (resp) {
                if (resp.errno == "0") {
                    // 修改成功
                    alert("修改成功")
                    window.location.reload()
                }else {
                    alert(resp.errmsg)
                }
            }
        })
    })
})